package cn.gok.thread;

import java.util.Random;

public class ProduceThread extends Thread {
    //共享数据
    private WareHouse wareHouse;

    public ProduceThread(WareHouse wareHouse) {
        this.wareHouse = wareHouse;
    }

    private Random rd = new Random();

    @Override
    public void run() {
        while (true) {
            String product = "商品" + rd.nextInt(1000);
            //将新商品放入仓库
            synchronized (wareHouse) {
                wareHouse.produce(product);
                //生产完商品后通知消费对象解除等待
                wareHouse.notifyAll();
            }
            try {
                Thread.sleep(rd.nextInt(1500));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
